package com.yikaiye.android.yikaiye.ui.activity;

import android.annotation.SuppressLint;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.base.BaseViewModel;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ActivityDesBinding;
import com.yikaiye.android.yikaiye.http.ApiFactory;

/**
 * 文字介绍
 */
public class DesActivity extends BaseActivity<ActivityDesBinding, BaseViewModel> {

    private String title;

    @Override
    protected BaseViewModel bindModel() {
        return getViewModel(this, BaseViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void initData() {
        initWebView();
        if (null != getIntent().getExtras()) {
            title = getIntent().getExtras().getString(Constant.NAME_KEY);
            initTitleBar(title);
        } else {
            initTitleBar();
        }
        loadUrl();
    }

    private void loadUrl() {
        String url = "";
        if (title.equals(getString(R.string.privacy_policy))) {
            url = ApiFactory.PRIVACY_AGREEMENT;
        } else if (title.equals(getString(R.string.user_agreement))) {
            url = ApiFactory.USER_AGREEMENT;
        } else if (title.equals(getString(R.string.qi_ming))) {
            url = ApiFactory.TO_NAME_URL;
        } else if (title.equals(getString(R.string.he_ming))) {
            url = ApiFactory.CHECK_NAME_URL;
        }
        binding.webView.loadUrl(url);
    }

    @SuppressLint("SetJavaScriptEnabled")
    public void initWebView() {
        WebSettings settings = binding.webView.getSettings();
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setDomStorageEnabled(true);
        settings.setDefaultTextEncodingName("UTF-8");
        // 是否可访问Content Provider的资源，默认值 true
        settings.setAllowContentAccess(true);
        // 是否可访问本地文件，默认值 true
        settings.setAllowFileAccess(true);
        // 是否允许通过file url加载的Javascript读取本地文件，默认值 false
        settings.setAllowFileAccessFromFileURLs(false);
        // 是否允许通过file url加载的Javascript读取全部资源(包括文件,http,https)，默认值 false
        settings.setAllowUniversalAccessFromFileURLs(false);
        //开启JavaScript支持
        settings.setJavaScriptEnabled(true);
        // 支持缩放
        settings.setSupportZoom(true);
//        //辅助WebView设置处理关于页面跳转，页面请求等操作
//        binding.webView.setWebViewClient(new MyWebViewClient());
//        //辅助WebView处理图片上传操作
//        binding.webView.setWebChromeClient(new MyChromeWebClient());
        binding.webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    binding.progressBar.setVisibility(View.GONE);
                } else {
                    binding.progressBar.setVisibility(View.VISIBLE);
                    binding.progressBar.setProgress(newProgress);
                }
            }
        });
    }
}